package com.kfh.ybracelet.sim;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.kfh.ybracelet.R;
import com.kfh.ybracelet.adapter.OrderListAdapter;
import com.kfh.ybracelet.base.BaseActivity;
import com.kfh.ybracelet.net.HttpUtil;
import com.kfh.ybracelet.utils.Constant;
import com.kfh.ybracelet.utils.DensityUtil;
import com.kfh.ybracelet.utils.LogUtil;
import com.kfh.ybracelet.utils.SpacesItemDecoration;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * 订单列表页面
 * Created by Aaron on 16/9/29.
 */
public class OrderListActivity extends BaseActivity {

    private RecyclerView rv_list;
    private Intent mIntent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_order_list);

        initView();

        try {
            getOrderList();
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    private void initView() {

        ImageView iv_left = (ImageView) findViewById(R.id.iv_left);
        iv_left.setOnClickListener(this);

        TextView tv_title = (TextView) findViewById(R.id.tv_title);
        tv_title.setText(getResources().getString(R.string.order_list));

        rv_list = (RecyclerView) findViewById(R.id.rv_list);

        rv_list.setHasFixedSize(true);
//        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 4);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rv_list.setLayoutManager(linearLayoutManager);
        rv_list.setItemAnimator(new DefaultItemAnimator());
        rv_list.addItemDecoration(new SpacesItemDecoration(DensityUtil.dip2px(this, 10)));
//        rv_list.addItemDecoration(new SpacesItemDecoration(DensityUtil.dip2px(this, 30), this, LinearLayoutManager.VERTICAL));

    }

    /**
     * 获取订单信息
     */
    private void getOrderList() throws JSONException {

        JSONObject request = new JSONObject();
        request.put("token", sp.getString(Constant.TOKEN, ""));

        HttpUtil.setPostRequest("请求中...", this, Constant.WEB_ROOT_PAY + Constant.PAYHISTORY,
                request.toString(), getClass().getSimpleName(), new HttpUtil.SuccessListener() {
                    @Override
                    public void setOnSuccessListener(String str) throws JSONException {

//                        try {

                        JSONObject resutl = new JSONObject(str);

//                            if (resutl.getInt("code") == Constant.STATUS_SUCCESS) {

                        JSONArray list = resutl.getJSONArray("list");

                        showOrderListInfo(list);
//                            } else {
//                                Constant.showErrorMsg(resutl.getInt("code"), OrderListActivity.this);
//                            }

//                        } catch (Exception e) {
//                            e.printStackTrace();
//                            showToast("数据格式错误");
//                        }

                    }
                }, null);

    }

    private final String TAG = getClass().getSimpleName();

    private void showOrderListInfo(final JSONArray list) {

        if (list.length() < 1) {
            showToast("未查询到订单信息");
            return;
        }

        OrderListAdapter orderListAdapter = new OrderListAdapter(this, list);
        rv_list.setAdapter(orderListAdapter);

        orderListAdapter.setItemClickListener(new OrderListAdapter.ItemClickListener() {
            @Override
            public void onclick(int position) {

                try {
                    JSONObject object = (JSONObject) list.get(position);

                    String body = object.getString("body");

                    JSONObject jsonObject = new JSONObject(body);
                    if (TextUtils.isEmpty(jsonObject.getString("address"))) {//充值订单, 查看详情

                        LogUtil.e(TAG, "这是充值 订单" + position);

                        String phoneNum = jsonObject.getString("phoneNum");
                        LogUtil.e(TAG, "点击的position:" + position + ",手机号码:" + phoneNum);
                        mIntent = new Intent(OrderListActivity.this, OrderInfoActivity.class);
                        mIntent.putExtra("phoneNum", phoneNum);
                        startActivity(mIntent);

                    } else {        //新开卡订单, 查询物流信息

                        LogUtil.e(TAG, "这是新开卡 订单" + position);

                        String tradeNo = object.getString("tradeNo");//订单号
                        mIntent = new Intent(OrderListActivity.this, LogisticsActivity.class);
                        mIntent.putExtra("tradeNo", tradeNo);
                        startActivity(mIntent);

                    }

//                    String address = jsonObject.getString("address");

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });

    }

    @Override
    public void widgetClick(View v) {

        switch (v.getId()) {

            case R.id.iv_left:
                finish();
                break;

        }

    }


}
